/**
 * Created by x on 2017/7/3.
 */
var mongoose = require("mongoose");

// var db_uri = "mongodb://127.0.0.1/factory";

var db_uri = "mongodb://factory_root:factory_root@127.0.0.1/factory";

mongoose.Promise = global.Promise;

var db = mongoose.createConnection(db_uri);

// var UserSchemma = new mongoose.Schema({
//     nickname:{type:String,default:'new user'},
//     regTime:{type:Date,default:Date.now}
//
// });
// //静态方法
// UserSchemma.statics.findByName = function (name,cb) {
//     this.find({nickname:name},function (err,doc) {
//         cb(err,doc);
//     });
// }
//
// // 实例方法
// UserSchemma.methods.print = function () {
//     console.log("name:",this.regTime);
// }

var Product = new mongoose.Schema({
    cpu_id:{type:String,unique: true },
    raw_sn:String,
    full_sn:String,
    bron_time:{type:Date,default:Date.now},
    product_type:Number,
    product_hw_ver:Number,
    ip_addr:String,
});


//静态方法
Product.statics.findByCPUID = function (cb,cpu_id) {
    this.find({cpu_id:cpu_id},function(err,doc)
    {
        cb(err,doc);
    });
}

Product.statics.item_nums = function (cb) {
    function  count_num (err,dat) {
        console.log(dat);
    }
    cb(0,10);
    // this.count("*",count_num);
}

// 实例方法
Product.methods.insertOne = function (cb,product_type,cpu_id,sn1,sn2,index,hw_ver) {
    this.cpu_id = cpu_id;
    this.raw_sn = sn1;
    this.full_sn = sn2;
    this.product_hw_ver  = hw_ver;
    this.product_type    = product_type;
    this.save(function (err) {
        cb(err);
    });
}

// db.once('open',function()
// {
//     console.log("open!!");
//     var Product_obj =  db.model('Product',Product);
//     var product  = new Product_obj();
//
//     // function opt_check(err) {
//     //
//     //     function num_all(err,num)
//     //     {
//     //         console.log(num)
//     //     }
//     //     Product_obj.item_nums(num_all);
//     //
//     //     if(err){console.log("insert data err!"); return;}
//     //     else    console.log("insert data ok!");
//     // }
//     //
//     // function  find_result(err,doc) {
//     //     if(err) {console.log("find_result err!"); return;}
//     //     else    console.log("find ok\n",doc);
//     // }
//     //
//     // var Product_obj =  db.model('Product',Product);
//     // var product  = new Product_obj();
//     // cpu_id = "1234567895545456788";
//     // sn1 = "1234";
//     // sn2 = "5678";
//     // type_ = 0;
//     // hw_version = 200;
//     // product.insertOne(opt_check,type_,cpu_id,sn1,sn2,hw_version);
//     //
//     // Product_obj.findByCPUID(find_result,"1234567895");
//
// });

module.exports = function () {

    var Product = new mongoose.Schema({
        cpu_id:{type:String,unique: true },
        raw_sn:String,
        full_sn:String,
        bron_time:{type:Date,default:Date.now},
        product_type:Number,
        product_hw_ver:Number,
    });


//静态方法
    Product.statics.findByCPUID = function (cb,cpu_id) {
        this.find({cpu_id:cpu_id},function(err,doc)
        {
            cb(err,doc);
        });
    }

    Product.statics.item_nums = function (cb) {
        console.log("Product.statics.item_num");
        this.count("*",cb);
    }

// 实例方法
    Product.methods.insertOne = function (cb,product_type,cpu_id,sn1,sn2,index,hw_ver,ip) {

        this.cpu_id = cpu_id;
        this.raw_sn = sn1;
        this.full_sn = sn2;
        this.product_hw_ver  = hw_ver;
        this.product_type    = product_type;
        this.ip_addr = ip;
        this.save(function (err) {
            cb(err);
        });
    }


    db.once('open',function(){
        console.log("open!!");
    });


    var Product_obj =  db.model('Product',Product);
    var product  = new Product_obj();

    const ret = {
        insert_dat: product.insertOne,
        total_num:  Product_obj.item_nums,
    };

    return ret;

}




